﻿
using Dapper;
using HBase;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;

namespace hCommand 
{
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class StockAddBSEngine<CS> : GkTransaction where CS : AbsCoreStock
    {
        public string boxCode;
        public int locId;
       
        protected override TransRes Execute()
        {

            CoreWhLoc loc = getById<CoreWhLoc>(locId);
            if (loc.type>0) {

                if (loc.maxStock - loc.countStock - loc.workingCount() == 0) {
                    return new TransRes(locId + "已经满!");
                }

            }

            before();
            checkStock();
            CS cs = createCS();
            cs.boxCode = boxCode;
            cs.locId = locId;
            cs.status = (int)StockStatus.库存;
            if (cs.id == 0)
            {

               insert(cs);
            }
            else
            {
                update(cs);
            }
            if (loc.type > 0) {
                LocCoreUtil.UpdataWhLoc(this, locId, 0);
            }
                return afterIntask(  cs, loc);
        }

        protected virtual TransRes afterIntask ( CS cs, CoreWhLoc loc)
        {
            return new TransRes();
        }
        protected virtual void before( )
        {
        }
        public abstract CS createCS( );



        public virtual void checkStock( )
        {
            string sql = @"select * from Core_stock where  "+ AbsCoreStock.isStoreStr + "   and box_Code=@boxCode  ";
   
            List<CS> stockList = query<CS>(sql, new
            {
                boxCode
            } ) ;
            if (stockList.Count != 0)
            {
                string info = boxCode + "重复的库位异常stock!";
                throw new Exception(info);
            }

        }
    }
}
